#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>

void GetUniqueTempFileName(const std::string& basename,
		                           std::string& tempname)
{
    int iRes;
    do 
    {
        time_t tmCurrent = time(NULL);
        std::stringstream strmName;
        strmName << basename << "_" << tmCurrent;
        tempname = strmName.str();

        struct stat st;
        iRes = stat(tempname.c_str(),
                    &st);
    }
    while( 0 == iRes );
}

void mktempfile(const std::string& basename, 
            std::string& tempname)
{
   char* temp="_XXXXXX";
   mktemp(temp);

   tempname = basename;
   tempname.append(temp);

}

int main()
{
    std::string basename("./abc");
    std::string tempname;

    GetUniqueTempFileName(basename, tempname);
    std::cout << tempname << std::endl;
    GetUniqueTempFileName(basename, tempname);
    std::cout << tempname << std::endl;
    GetUniqueTempFileName(basename, tempname);
    std::cout << tempname << std::endl;
    GetUniqueTempFileName(basename, tempname);
    std::cout << tempname << std::endl;
    GetUniqueTempFileName(basename, tempname);
    std::cout << tempname << std::endl;
    GetUniqueTempFileName(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;
    mktempfile(basename, tempname);
    std::cout << tempname << std::endl;

    return 0;
}
